import time
import requests


def get_headers(token, user_address):
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Encoding": "gzip, deflate, br, zstd",
        "Accept-Language": "en-US",
        "Language": "en",
        "Manageid": "100004",
        "Origin": "https://secwarex.io",
        "Referer": "https://secwarex.io/",
        "Sec-Ch-Ua": "\"Google Chrome\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\"",
        "Sec-Ch-Ua-Mobile": "?0",
        "Sec-Ch-Ua-Platform": "\"Windows\"",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-site",
        "Token": token,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
        "X-Address": user_address,
        "X-Project": "secwarex"
    }
    return headers


def get_img_task_info(task_id, theme_id, token, user_address):
    time.sleep(2)
    data = {
        "task_id": task_id,
        "user_address": user_address,
        "theme_id": theme_id
    }
    try:
        response = requests.post("https://tgbot.secwarex.io/imgTask/getImgTaskInfo", json=data,
                                 headers=get_headers(token, user_address), verify=False)
        if response.status_code == 200:
            return response.json()
        else:
            print("Failed to fetch data. Status code:", response.status_code)
            return None
    except Exception as e:
        print("An error occurred:", e)
        return None


def submit_img_task_answer(task_id, answer, token, user_address):
    url = "https://tgbot.secwarex.io/imgTask/submitImgTaskAnswer"
    data = {
        "task_id": task_id,
        "opt_id": answer
    }
    try:
        response = requests.post(url, json=data, headers=get_headers(token, user_address), verify=False)
        if response.status_code == 200:
            return response.json()
        else:
            print("Failed to submit answer. Status code:", response.status_code)
            return None
    except Exception as e:
        print("An error occurred:", e)
        return None


def get_theme_ids(token, user_address):
    theme_id_list = []
    url = f"https://api.secwarex.io/api/v1/theme/list?chainId=1&type=HOT&userAddress={user_address}"

    headers = get_headers(token, user_address)
    response = requests.get(url, headers=headers, verify=False)

    if response.status_code == 200:
        data = response.json()
        result = data.get("result", {})
        records = result.get("records", [])
        for record in records:
            theme_id_list.append(record.get("themeId"))

    url = f"https://api.secwarex.io/api/v1/theme/list?chainId=1&type=THEME&userAddress="
    response = requests.get(url, headers=get_headers(token, user_address), verify=False)
    if response.status_code == 200:
        data = response.json()
        result = data.get("result", {})
        records = result.get("records", [])
        for record in records:
            theme_id_list.append(record.get("themeId"))

    return theme_id_list


def get_user_info(token, user_address):
    url = f"https://api.secwarex.io/api/v1/user/integral/info?userAddress={user_address}"
    response = requests.get(url, headers=get_headers(token, user_address), verify=False)
    return response.status_code == 200
